package com.dingreading.cloud.admin.service.impl;

import com.dingreading.cloud.admin.entity.Course;
import com.dingreading.cloud.admin.entity.table.CourseTableDef;
import com.dingreading.cloud.admin.mapper.CourseMapper;
import com.dingreading.cloud.admin.service.CourseService;
import com.dingreading.cloud.common.util.NepUtil;
import com.dingreading.cloud.common.util.PageUtil;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryCondition;
import com.mybatisflex.core.query.QueryMethods;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.core.update.UpdateChain;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

/**
 * 服务层实现。
 *
 * @author Administrator
 * @since 2023-11-22
 */
@Service
public class CourseServiceImpl extends BaseServiceImpl<CourseMapper, Course, CourseTableDef> implements CourseService {

    @Override
    protected CourseTableDef getTable() {
        return CourseTableDef.course;
    }

    @Override
    public Page<Course> pageList(PageUtil pageUtil, Long mainId, Long typeId, String keyword, Integer enabled, Integer hotStatus) {
        QueryCondition condition = QueryCondition.createEmpty();
        if (mainId != null)
            condition.and(table.mainTypeId.eq(mainId));
        if (typeId != null)
            condition.and(table.typeId.eq(typeId));
        if (StringUtils.isNotBlank(keyword))
            condition.and(table.name.like(keyword));
        if (enabled != null)
            condition.and(table.enabled.eq(enabled));

        QueryWrapper wrapper = QueryWrapper.create()
                .where(condition)
                .orderBy(table.orderNum.asc());

        return getMapper().paginate(Page.of(pageUtil.getPage(), pageUtil.getRows()), wrapper);
    }

    @Override
    public int getMaxShowOrder(Long courseTypeId) {
        QueryWrapper wrapper = QueryWrapper.create()
                .select(QueryMethods.max(table.orderNum))
                .where(table.typeId.eq(courseTypeId));

        Integer integer = getMapper().selectObjectByQueryAs(wrapper, Integer.class);

        return NepUtil.nullToZero(integer);
    }

    @Override
    public void updateEnabled(Long id, Integer enabled) {
        UpdateChain.of(Course.class)
                .set(table.enabled, enabled)
                .where(table.id.eq(id))
                .update();
    }

    @Override
    public void updateHotStatus(Long id, Integer hotStatus) {
        UpdateChain.of(Course.class)
                .set(table.hotStatus, hotStatus)
                .where(table.id.eq(id))
                .update();
    }

    @Override
    public long dropCourse(Long id) {
        return getMapper().deleteByCondition(table.id.eq(id));
    }

    @Override
    public Course getGtOrderNum(Long courseTypeId, Integer orderNum) {
        QueryCondition condition = QueryCondition.createEmpty();
        condition.and(table.typeId.eq(courseTypeId))
                .and(table.orderNum.gt(orderNum));

        QueryWrapper wrapper = QueryWrapper.create()
                .where(condition)
                .orderBy(table.orderNum.asc());
        return getMapper().selectOneByQuery(wrapper);
    }

    @Override
    public Course getLtOrderNum(Long courseTypeId, Integer orderNum) {
        QueryCondition condition = QueryCondition.createEmpty();
        condition.and(table.typeId.eq(courseTypeId))
                .and(table.orderNum.lt(orderNum));

        QueryWrapper wrapper = QueryWrapper.create()
                .where(condition)
                .orderBy(table.orderNum.desc());
        return getMapper().selectOneByQuery(wrapper);
    }

    @Override
    public void updateOrderNum(Long id, Integer orderNum) {
        UpdateChain.of(Course.class)
                .set(table.orderNum, orderNum)
                .where(table.id.eq(id))
                .update();
    }

    @Override
    public long getByTypeIdCnt(Long typeId) {
        QueryCondition condition = QueryCondition.createEmpty();
        condition.and(table.typeId.eq(typeId).or(table.mainTypeId.eq(typeId)));
        return getMapper().selectCountByCondition(condition);
    }

    @Override
    public void updateUrl(Long id, String url) {
        UpdateChain.of(Course.class)
                .set(table.url, url)
                .where(table.id.eq(id))
                .update();
    }
}
